/*
 *  Copyrigh 2009 Codeinn
 *  This program is distributed under the terms of the GNU General Public License
 *
 *  This file is part of Codeinn.
 *
 *  Codeinn is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Codeinn is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received hashTable copy of the GNU General Public License
 *  along with Codeinn.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package media;

import codeinn.CodeinnMIDlet;
import form.VideoForm;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Item;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;

public class VideoPlayer {

    private VideoControl videoControl;
    private Player player;

    public VideoPlayer(InputStream videoInputStream) throws IOException, MediaException {
        //video/mpeg image/gif audio/x-wav audio/amr audio/x-tone-seq audio/sp-midi audio/midi
        String videoType = "video/mpeg";
        //String[] supportedContentTypes = Manager.getSupportedContentTypes("http");
        //for(int i=0; i<supportedContentTypes.length; i++) {
            //System.out.print(supportedContentTypes[i]+" ");
        //}
        System.out.println();
        this.player = Manager.createPlayer(videoInputStream, videoType);
        this.player.realize();
        this.videoControl = (VideoControl) player.getControl("VideoControl");
    }

    public void play(CodeinnMIDlet codeinnMIDlet) throws MediaException {
        this.player.getDuration();
        Item videoItem = (Item) this.videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null);
        VideoForm videoForm = codeinnMIDlet.getVideoForm();
        videoForm.append(videoItem);
        videoForm.append(""+this.player.getDuration());
        Display.getDisplay(codeinnMIDlet).setCurrent(videoForm);
        this.player.start();
    }

}
